package com.ruoyi.web.controller.system;
import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HatchetAnnouncement;
import com.ruoyi.system.service.IHatchetAnnouncementService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 公告管理 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-21
 */
@Controller
@RequestMapping("/system/hatchetAnnouncement")
public class HatchetAnnouncementController extends BaseController
{
    private String prefix = "system/hatchetAnnouncement";
	
	@Autowired
	private IHatchetAnnouncementService hatchetAnnouncementService;
	
	@RequiresPermissions("system:hatchetAnnouncement:view")
	@GetMapping()
	public String hatchetAnnouncement()
	{
	    return prefix + "/hatchetAnnouncement";
	}
	
	/**
	 * 查询公告管理列表
	 */
	@RequiresPermissions("system:hatchetAnnouncement:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HatchetAnnouncement hatchetAnnouncement)
	{
		startPage();
        List<HatchetAnnouncement> list = hatchetAnnouncementService.selectHatchetAnnouncementList(hatchetAnnouncement);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出公告管理列表
	 */
	@RequiresPermissions("system:hatchetAnnouncement:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HatchetAnnouncement hatchetAnnouncement)
    {
    	List<HatchetAnnouncement> list = hatchetAnnouncementService.selectHatchetAnnouncementList(hatchetAnnouncement);
        ExcelUtil<HatchetAnnouncement> util = new ExcelUtil<HatchetAnnouncement>(HatchetAnnouncement.class);
        return util.exportExcel(list, "hatchetAnnouncement");
    }
	
	/**
	 * 新增公告管理
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存公告管理
	 */
	@RequiresPermissions("system:hatchetAnnouncement:add")
	@Log(title = "公告管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HatchetAnnouncement hatchetAnnouncement) {
		hatchetAnnouncement.setId(StringUtils.randomUUID());
		hatchetAnnouncement.setUpdateDate(new Date());
		hatchetAnnouncement.setStatus("10A");
		if (StringUtils.isNotEmpty(hatchetAnnouncement.getStrEffectiveFromTime())) {
			hatchetAnnouncement.setEffectiveFromTime(
					DateUtils.dateTime("yyyy-MM-dd", hatchetAnnouncement.getStrEffectiveFromTime()));
		}
		if (StringUtils.isNotEmpty(hatchetAnnouncement.getStrEffectiveToTime())) {
			hatchetAnnouncement
					.setEffectiveToTime(DateUtils.dateTime("yyyy-MM-dd", hatchetAnnouncement.getStrEffectiveToTime()));
		}
		return toAjax(hatchetAnnouncementService.insertHatchetAnnouncement(hatchetAnnouncement));
	}

	/**
	 * 修改公告管理
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap) {
		HatchetAnnouncement hatchetAnnouncement = hatchetAnnouncementService.selectHatchetAnnouncementById(id);
		if (StringUtils.isNotNull(hatchetAnnouncement.getEffectiveFromTime())) {
			hatchetAnnouncement.setStrEffectiveFromTime(
					DateUtils.parseDateToStr("yyyy-MM-dd", hatchetAnnouncement.getEffectiveFromTime()));
		}
		if (StringUtils.isNotNull(hatchetAnnouncement.getEffectiveToTime())) {
			hatchetAnnouncement.setStrEffectiveToTime(
					DateUtils.parseDateToStr("yyyy-MM-dd", hatchetAnnouncement.getEffectiveToTime()));
		}
		mmap.put("hatchetAnnouncement", hatchetAnnouncement);
		return prefix + "/edit";
	}
	
	/**
	 * 修改保存公告管理
	 */
	@RequiresPermissions("system:hatchetAnnouncement:edit")
	@Log(title = "公告管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(HatchetAnnouncement hatchetAnnouncement) {
		hatchetAnnouncement.setUpdateDate(new Date());
		if (StringUtils.isNotEmpty(hatchetAnnouncement.getStrEffectiveFromTime())) {
			hatchetAnnouncement.setEffectiveFromTime(
					DateUtils.dateTime("yyyy-MM-dd", hatchetAnnouncement.getStrEffectiveFromTime()));
		}
		if (StringUtils.isNotEmpty(hatchetAnnouncement.getStrEffectiveToTime())) {
			hatchetAnnouncement
					.setEffectiveToTime(DateUtils.dateTime("yyyy-MM-dd", hatchetAnnouncement.getStrEffectiveToTime()));
		}
		return toAjax(hatchetAnnouncementService.updateHatchetAnnouncement(hatchetAnnouncement));
	}
	
	/**
	 * 删除公告管理
	 */
	@RequiresPermissions("system:hatchetAnnouncement:remove")
	@Log(title = "公告管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(hatchetAnnouncementService.deleteHatchetAnnouncementByIds(ids));
	}

	/**
     * 公告状态修改
     */
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:hatchetAnnouncement:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
	public AjaxResult changeStatus(HatchetAnnouncement hatchetAnnouncement) {
		hatchetAnnouncement.setUpdateDate(new Date());
		hatchetAnnouncement.setUpdateUser(getLoginName());
		return toAjax(hatchetAnnouncementService.updateHatchetAnnouncement(hatchetAnnouncement));
	}
	
}
